package com.gamebox.view;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import com.gamebox.dao.GameDao;
import com.gamebox.javaBean.Game;
import com.gamebox.javaBean.User;

@SuppressWarnings("serial")
public class AdminGameTable extends AbstractTableModel {

	private String[] columnNames = { "name", "label", "path", "clickNum","score","playerNum" };
	private ArrayList<ArrayList<String>> records;
	private JTable table = null;
	private ArrayList<Game> games;
	private GameDao gameDao;
	private User user;

	public AdminGameTable(ArrayList<Game> games) {
		this.games = games;
		table = new JTable(this);
		gameDao = new GameDao();
		init(games);
		table.addMouseListener(new DoubleClick());
	}
	
	

	public void setGames(ArrayList<Game> games) {
		this.games = games;
		init(games);
		table.repaint();
		table.validate();
		table.updateUI();
	}
	

	private void init(ArrayList<Game> games) {
		records = new ArrayList<ArrayList<String>>();
		for (int i = 0; i < games.size(); i++) {
			Game game = games.get(i);
			ArrayList<String> record = new ArrayList<String>();
			record.add(game.getName());
			record.add(game.getLabel());
			record.add(game.getPath());
			record.add(String.valueOf(game.getClickNum()));
			record.add(String.valueOf(game.getScore()));
			record.add(String.valueOf(game.getPlayerNum()));
			records.add(record);
		}
	}

	@Override
	public int getRowCount() {
		return records.size();
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {

		return records.get(rowIndex).get(columnIndex);

	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}
	class DoubleClick extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            if(e.getClickCount() ==2) {
            	int i = AdminGameTable.this.table.getSelectedRow();
            	Game game = games.get(i);
            	new GameDetailDialog(game,user);
            }
        }
    }
}
